import { v4 as uuidv4 } from 'uuid'
import {
  type IMessageParser,
  type ChatMessage,
  AIGCStatus,
  type MessageContent,
  MessageContentType,
  MessageType
} from './interface'

import { escapeHTML, processContent } from './lib'
import { parseMarkdownToHTML } from './markdown-parser'

//定义一个map 映射 不同content_type 取值的key
const contentTypeMap: Record<string, string> = {
  text: 'content',
  image: 'image_url',
  progress: 'progress',
  from_input: 'fromInput',
  from_output: 'fromOutput',
  recommend: 'recommend',
  reference: 'reference'
}

const supportedContentTypes: string[] = ['text']

export class IFlyParser implements IMessageParser {
  sessionIdFromResp(respData: any): string {
    return respData?.replay_data?.payload?.sessionId
  }
  messageListFromHistoryMessageResp(respData: any): ChatMessage[] {
    const chatPairs = respData || []
    const messageList: ChatMessage[] = []

    chatPairs.forEach((item: any) => {
      const userContentRowDatas = item.user.filter((content: any) => {
        return this.isSupportedContentType(content)
      })

      const assistantContentRowDatas = item.assistant.filter((content: any) => {
        return this.isSupportedContentType(content)
      })

      const userContents = userContentRowDatas.map((rowData: any) => {
        return this.messageContentWithRowData(rowData)
      })
      const assistantContents = assistantContentRowDatas.map((rowData: any) => {
        return this.messageContentWithRowData(rowData)
      })

      const userMessage: ChatMessage = {
        messageId: uuidv4(),
        contentList: userContents,
        messageType: MessageType.Text,
        isSend: true
      }

      const assistantMessage: ChatMessage = {
        messageId: uuidv4(),
        contentList: assistantContents,
        messageType: MessageType.Text,
        isSend: false
      }
      messageList.push(userMessage)
      messageList.push(assistantMessage)
    })

    return messageList
  }

  isSupportedContentType(content: any): boolean {
    return supportedContentTypes.includes(content?.content_type || '')
  }
  isTypeWriterContent(content: any): boolean {
    if (content?.content_type === MessageContentType.Text) {
      return true
    }
    return false
  }
  aigcTypeWriterContentCacheData(content: any): string {
    if (content?.content_type === MessageContentType.Text) {
      return content.content
    }
    return ''
  }

  contentRowDataListFromResp(respData: any): any[] {
    const contentRowDataList = respData?.replay_data?.payload?.choices?.text
    if (!contentRowDataList || contentRowDataList.length === 0) {
      return []
    }

    return contentRowDataList
  }

  messageContentWithRowData(contentItem: any): MessageContent {
    const contentKey = contentTypeMap[contentItem?.content_type] || 'content'

    let content = contentItem?.content
    if (contentItem[contentKey]) {
      content = contentItem[contentKey]
    }

    if (contentItem?.content_type === MessageContentType.Text) {
      content = parseMarkdownToHTML(processContent(content))
    }

    const target: MessageContent = {
      rowData: contentItem,
      content: content,
      contentType: contentItem?.content_type
    }
    return target
  }

  taskStatusFromResp(respData: any): AIGCStatus {
    const status = respData?.replay_data?.payload?.choices?.status
    let target = AIGCStatus.InProgress

    switch (status) {
      case 0: {
        target = AIGCStatus.Started
        break
      }
      case 2: {
        target = AIGCStatus.Completed
        break
      }
      default:
        break
    }

    return target
  }
}
